package com.exam.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.exam.exception.AdminNameIsExistException;
import com.exam.pojo.Admin;
import com.exam.pojo.Paging;
import com.exam.service.AdminService;
import com.google.gson.Gson;

/**
 * @author 郭荣锋
 * 管理员管理控制器
 */
@Controller
@RequestMapping("/AdminManage")
public class AdminManage {

	@Resource(name="mv")
	private ModelAndView mv;
	@Resource(name="adminServiceImpl")
	private AdminService adminService;
	@Resource(name="admin")
	private Admin admin;
	@Resource(name="gson")
	private Gson gson ;
	
	/**
	 * 管理员列表页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/admin-list")
	public ModelAndView admin_list(HttpServletRequest request,HttpServletResponse response) {
		//List<Admin> adminList = adminService.getAllAdmin();
		
		//mv.addObject("adminList",adminList);
		mv.setViewName("admin/admin-list");
		return mv;
	}
	
	/**
	 * 添加管理员页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/admin-add")
	public ModelAndView admin_add(HttpServletRequest request,HttpServletResponse response) {
		List<Admin> adminList = adminService.getAllAdmin();
		mv.addObject("adminList",adminList);
		mv.setViewName("admin/admin-add");
		return mv;
	}
	
	/**
	 * 获取当前登录的管理员身份
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/getNowLoginAdmin")
	public void getNowLoginAdmin(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter printWriter = response.getWriter();
		Integer adminId = (Integer)request.getSession().getAttribute("adminId");
		if (adminId==1) { //如果当前登录的管理员是超级管理员
			printWriter.write("{\"res\":\"1\"}");
		} else {
			printWriter.write("{\"res\":\"0\"}");
		}
		printWriter.close();
	}
	
	/**
	 * 添加管理员
	 * @throws IOException 
	 */
	@RequestMapping("/addAdmin")
	public void addAdmin(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter printWriter = response.getWriter();
		String adminName = request.getParameter("adminName");
		String password = request.getParameter("password");
		admin.setAdminName(adminName);
		admin.setPassword(password);
		admin.setState(1);
		admin.setStatus(3);
		try {
			adminService.addAdmin(admin);
			printWriter.write("{\"res\":\"1\"}");
		} catch (AdminNameIsExistException e) { //已存在同名管理员
			printWriter.write("{\"res\":\"0\"}");
		}
		printWriter.close();
	}
	
	/**
	 * 停用管理员
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	@RequestMapping("/stopAdmin")
	public void stopAmin(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter printWriter = response.getWriter();
		//Integer adminId = Integer.parseInt(request.getParameter("adminId"));
		String adminName = request.getParameter("adminName");
		if(1 == adminService.getAdminByName(adminName).getId()) { //如果是超级管理员的话
			printWriter.write("{\"res\":\"0\"}");
		} else {
			adminService.stopAdmin(adminName);
			printWriter.write("{\"res\":\"1\"}");
		}
		printWriter.close();
	}
	
	/**
	 * 验证管理员身份是否拥有编辑管理员的资格
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	@RequestMapping("/validate")
	public void validate(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter printWriter = response.getWriter();
		Integer adminId = (Integer)request.getSession().getAttribute("adminId");
		if(1==adminId) { //如果是超级管理员
			printWriter.write("{\"res\":\"1\"}");
		} else {
			printWriter.write("{\"res\":\"0\"}");
		}
		printWriter.close();
	}
	
	/**
	 * 管理员编辑页面
	 * @param request
	 * @param response
	 */
	@RequestMapping("/admin-update")
	public ModelAndView admin_update(HttpServletRequest request,HttpServletResponse response) {
		Integer adminId = Integer.parseInt(request.getParameter("adminId"));
		String adminName = request.getParameter("adminName");
		String password = request.getParameter("password");
		mv.addObject("adminName",adminName);
		mv.addObject("adminId",adminId);
		mv.addObject("password",password);
		mv.setViewName("admin/admin-update");
		return mv;
	}
	
	/**
	 * 更新管理员信息
	 * @throws IOException 
	 */
	@RequestMapping("/updateAdmin")
	public void updateAdmin(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter printWriter = response.getWriter();
		Integer adminId = Integer.parseInt(request.getParameter("adminId"));
		String adminName = request.getParameter("adminName");
		String password = request.getParameter("password");
		admin.setAdminName(adminName);
		admin.setId(adminId);
		admin.setPassword(password);
		admin.setState(1);
		admin.setStatus(3);
		try {
			adminService.updateAdmin(admin);
			printWriter.write("{\"res\":\"1\"}");
		} catch (AdminNameIsExistException e) {
			printWriter.write("{\"res\":\"0\"}");
		}
		printWriter.close();
	}
	
	/**
	 * 以分页的形式获取管理员列表
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/getAdminByPaging")
	public void getAdminByPaging(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter printWriter = response.getWriter();
		Integer page = Integer.parseInt(request.getParameter("page")); //当前页
		Integer pageSize = Integer.parseInt(request.getParameter("pageSize")); //页面大小
		Paging<Admin> paging = adminService.getAdminByPaging(page,pageSize);
		String s = gson.toJson(paging);
		printWriter.write(s);
		printWriter.close();
	}
	
	/**
	 * 启用管理员
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	@RequestMapping("/startAdmin")
	public void startAdmin(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter printWriter = response.getWriter();
		String adminName = request.getParameter("adminName");
		adminService.startAdmin(adminName);
		printWriter.write("{\"res\":\"1\"}");
		printWriter.close();
	}
	
}
